package com.frxuexi.park2301

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.park2301.databinding.ActivityWebViewBinding
import com.tencent.smtt.sdk.WebView

@Route(path = "/app/WebViewActivity")
class WebViewActivity : BaseViewActivity<ActivityWebViewBinding>() {
    var wv:WebView?=null
    override fun initData() {

    }

    override fun initView() {
        wv=binding.web
        wv?.apply {
            loadUrl("file:///android_asset/test.html")
            settings.javaScriptEnabled=true//允许js交互
            //js调安卓步骤1
            addJavascriptInterface(this@WebViewActivity,"android")
        }

        binding.btnJs.setOnClickListener {
//            安卓调用js  1
            wv?.loadUrl("javascript:harmonyCallJS()")
        }
    }

//    js调安卓步骤2
    @JavascriptInterface
    fun harmonyosAgree():String{
        return "hello"
    }

    override fun onDestroy() {
        super.onDestroy()
        if(wv!=null){
            wv?.clearHistory()
            wv?.clearCache(true)
            wv?.destroy()
            wv=null
        }
    }

}